package com.education.platform.management.config;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;


/**
 * swagger文档
 * @author tomato
 */
@Profile({"dev","test"})
@Configuration
@EnableSwagger2
public class Swagger2Configure {


    // 定义分隔符
    private static final String splitor = ";";


    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                // base，最终调用接口后会和paths拼接在一起
                .pathMapping("/")
                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.education.platform.api.controller"))
                //多个包
                .apis(
                        Predicates.or(
                                RequestHandlerSelectors.basePackage("com.education.platform.management.controller"),
                                RequestHandlerSelectors.basePackage("com.education.platform.common.web")
                        ))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(Collections.singletonList(
                        new ParameterBuilder()
                                .name("token")
                                .description("token")
                                .modelRef(new ModelRef("string"))
                                .parameterType("header")
                                .required(false)
                                .build()
                ));


    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("后台接口")
                .description("后台接口")
                .termsOfServiceUrl("http:www.baidu.com")
                .contact("haha")
                .version("1.0")
                .build();
    }


}
